package de.uni.muenster.idw2;

import java.util.Collections;
import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import de.uni.muenster.idw2.data.Building;
import de.uni.muenster.idw2.data.Floor;
import de.uni.muenster.idw2.data.Room;

public class RouteSelector extends Activity{
	
	public static final String START_POINT = "startPoint";
	public static final String DESTINATION_POINT = "destinationPoint";
	
	private Building building;
	
	private Spinner spinnerStart;
	private Spinner spinnerDestination;
	private AutoCompleteTextView autoCompleteTextViewStart;
	private AutoCompleteTextView autoCompleteTextViewDestination;
	private Button button;

	private LinkedList<String> adapterItemsStart;
	private LinkedList<String> adapterItemsDestination;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.route_selector);
		
		/**
		 * load the builing
		 */
		
		building = Building.getBuilding();
		
		
		spinnerStart = (Spinner) findViewById(R.id.spinnerStart);
		spinnerDestination = (Spinner) findViewById(R.id.spinnerDestination);
		autoCompleteTextViewDestination = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextViewDestination);
		autoCompleteTextViewStart = (AutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextViewStart);
		button = (Button) findViewById(R.id.button1);
		
		/**
		 * load the start and dest points into the spinner
		 */
		if(building != null){
			adapterItemsStart = new LinkedList<String>();
			adapterItemsDestination = new LinkedList<String>();
			
			LinkedList<Floor> floors = building.getFloors();
			for (Floor floor : floors) {
				LinkedList<Room> rooms = floor.getRooms();
				for (Room room : rooms) {
					String name = room.getName();
					adapterItemsStart.add(name);
					adapterItemsDestination.add(name);
				}
			}
			
			Collections.sort(adapterItemsDestination);
			Collections.sort(adapterItemsStart);
			
			adapterItemsStart.addFirst("Current Location");
			
			ArrayAdapter adapterStart = new ArrayAdapter(this, android.R.layout.simple_spinner_item, adapterItemsStart);
			ArrayAdapter adapterDestination = new ArrayAdapter(this, android.R.layout.simple_spinner_item, adapterItemsDestination);
			
			adapterStart.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			adapterDestination.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			
			spinnerStart.setAdapter(adapterStart);
			spinnerDestination.setAdapter(adapterDestination);
			
			
			ArrayAdapter adapterStartAuto = new ArrayAdapter(this, R.layout.list_item, adapterItemsStart);
			ArrayAdapter adapterDestinationAuto = new ArrayAdapter(this, R.layout.list_item, adapterItemsDestination);
			
			autoCompleteTextViewStart.setAdapter(adapterStartAuto);
			autoCompleteTextViewDestination.setAdapter(adapterDestinationAuto);
			
			autoCompleteTextViewStart.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,	int after) {
				}
				
				@Override
				public void afterTextChanged(Editable s) {
//					updateSpinner();
				}
			});
			
			autoCompleteTextViewDestination.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,	int after) {
				}
				
				@Override
				public void afterTextChanged(Editable s) {
//					updateSpinner();
				}
			});
			
			spinnerStart.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					updateText();
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
			
			spinnerDestination.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					updateText();
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});
		}
		
	}
	
    public void startNavigation(View view){
    	Intent intent = new Intent(this, Navigation.class);
    	
    	String start = autoCompleteTextViewStart.getText().toString();
    	String dest = autoCompleteTextViewDestination.getText().toString();
    	
//    	for (Floor floor : building.getFloors()) {
//			for (Room room : floor.getRooms()) {
//				if (room.getName().equals(start)) {
//					start = Double.toString(room.getId());
//				}
//				if (room.getName().equals(dest)) {
//					dest = Double.toString(room.getId());
//				}
//			}
//		}
    	
    	intent.putExtra(START_POINT, start);
    	intent.putExtra(DESTINATION_POINT, dest);
    	
    	intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    	startActivity(intent);
    	finish();
    }
    
    private void updateSpinner(){
    	String startSpinnerText = autoCompleteTextViewStart.getText().toString();
    	String destinationSpinnerText = autoCompleteTextViewDestination.getText().toString();
    	
    	for(int i = 0; i < adapterItemsStart.size(); i++){
    		if(startSpinnerText.equals(adapterItemsStart.get(i))){
    			spinnerStart.setSelection(i);
    		}
    	}
    	
    	for(int i = 0; i < adapterItemsDestination.size(); i++){
    		if(destinationSpinnerText.equals(adapterItemsDestination.get(i))){
    			spinnerDestination.setSelection(i);
    		}
    	}
    }
    
    private void updateText(){
    	int startPos = spinnerStart.getSelectedItemPosition();
    	int destinationPos = spinnerDestination.getSelectedItemPosition();
    	
    	autoCompleteTextViewDestination.setText(adapterItemsDestination.get(destinationPos));
    	autoCompleteTextViewStart.setText(adapterItemsStart.get(startPos));
    	
    }
}
